package com.paat.common.passwordSecurity.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.paat.common.passwordSecurity.entity.PasswordSecurity;
import com.paat.common.passwordSecurity.mapper.PasswordSecurityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@Slf4j
public class PassWordController {

    @Resource
    PasswordSecurityMapper passwordSecurityMapper;

    @RequestMapping("/update")
    public JSONObject save(@RequestBody PasswordSecurity passwordSecurity){
        JSONObject object = new JSONObject();
        object.put("state",1);
        object.put("msg","更新失败");

        if(!StringUtils.hasText(passwordSecurity.getEmail())){
            object.put("msg","邮箱地址不能为空");
            return object;
        }

        if(!StringUtils.hasText(passwordSecurity.getPassWord())){
            object.put("msg","原密码不能为空");
            return object;
        }

        if(!StringUtils.hasText(passwordSecurity.getNewPassWord())){
            object.put("msg","新密码不能为空");
            return object;
        }

        int leave = passwordSecurityLevel(passwordSecurity.getNewPassWord());
        log.info("邮箱：{} 新密码等级：{}",passwordSecurity.getEmail(),leave);
        passwordSecurity.setLevel(leave);

        if(leave < 1){
            object.put("msg","密码为 6-12 位字母、数字或英文字符!");
            return object;
        }

        String time = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");

        passwordSecurity.setModifyUserId(0);
        passwordSecurity.setModifyTi(Long.valueOf(time));

        QueryWrapper<PasswordSecurity> query = Wrappers.query();
        query.eq(PasswordSecurity.EMAIL, passwordSecurity.getEmail());

        int flag = passwordSecurityMapper.selectCount(query);

        int result = 0;
        if(flag > 0){
            result =  passwordSecurityMapper.update(passwordSecurity,query);
        }else{
            passwordSecurity.setCreateUserId(0);
            passwordSecurity.setCreateTi(Long.valueOf(time));
            result = passwordSecurityMapper.insert(passwordSecurity);
        }
        if(result > 0){
            object.put("state",0);
            object.put("msg","更新成功");
        }
        return object;
    }

    public int passwordSecurityLevel(String password){
        int securityLevelFlag = 0;
        // Z = 字母       S = 数字           T = 特殊字符
        String regexZ = "[A-Z]+";
        String regexS = "^\\d+$";
        String regexT = "[~!@#$%^&*.]+";
        String regexZT = "[a-zA-Z~!@#$%^&*.]+";
        String regexZS = "[0-9A-Za-z]+";
        String regexST = "[\\d~!@#$%^&*.]*";
        String regexZST = "[\\da-zA-Z~!@#$%^&*.]+";

        if (password.length() < 6) {
            return securityLevelFlag;
        }
        if (password.matches(regexZ) || password.matches(regexS) || password.matches(regexT)){
            securityLevelFlag=1;    //lowercase
        }else if (password.matches(regexZT) || password.matches(regexZS) || password.matches(regexST)){
            securityLevelFlag=2;    //uppercase
        }else if (password.matches(regexZST)){
            securityLevelFlag=3;   //digital
        }
        return securityLevelFlag;
    }
}
